package com.example.test1;

import com.example.test1.assistant.Assistant;
import com.example.test1.assistant.MemeryAssistant;
import com.example.test1.assistant.SeparateChatAssistant;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.service.AiServices;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;

@SpringBootTest
public class ChatMemoryTast {
    @Autowired
    private QwenChatModel qwenChatModel;
    //测试1,简单的对话记忆实现,通过将   消息对象(包括UserMessage和ChatResponse)   作为参数传递给chat方法,实现了对话记忆
    @Test
    public void testChatMemory2() {
        //第一轮对话
        UserMessage userMessage1 = UserMessage.userMessage("我是环环");
        ChatResponse chatResponse1 = qwenChatModel.chat(userMessage1);
        AiMessage aiMessage1 = chatResponse1.aiMessage();
        //输出大语言模型的回复
        System.out.println(aiMessage1.text());
        //第二轮对话
        UserMessage userMessage2 = UserMessage.userMessage("你知道我是谁吗");
        ChatResponse chatResponse2 = qwenChatModel.chat(Arrays.asList(userMessage1,
                aiMessage1, userMessage2));  //发送给他一个队列
        AiMessage aiMessage2 = chatResponse2.aiMessage();
        //输出大语言模型的回复
        System.out.println(aiMessage2.text());
    }

    //测试2,通过AIAssistant接口,实现对话记忆
    @Test
    public void testChatMemory3() {
        //创建chatMemory,设置对话窗口大小为10,即最多保存10条消息
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
//创建AIService
        Assistant assistant = AiServices
                .builder(Assistant.class)
                .chatLanguageModel(qwenChatModel)
                .chatMemory(chatMemory)  //添加chatMemory字段
                .build();
        //调用service的接口,发送消息
        String answer1 = assistant.chat("我是环环");
        System.out.println(answer1);
        String answer2 = assistant.chat("我是谁");
        System.out.println(answer2);
    }

    //测试3--直接注入携带记忆区的AIService,不用在build构建了
    @Autowired
    private MemeryAssistant memeryAssistant;
    @Test
    public void testChatMemory4() {
        String answer1 = memeryAssistant.chat("我是环环");
        System.out.println(answer1);
        String answer2 = memeryAssistant.chat("我是谁");
        System.out.println(answer2);
    }

    //测试4--有隔离作用的记忆区,调用chat的时候,传入参数id作为主键值,区分不同的记忆区
    @Autowired
    private SeparateChatAssistant separateChatAssistant;  //直接注入助手对象
    @Test
    public void testChatMemory5() {
        String answer1 = separateChatAssistant.chat(1,"我是环环");
        System.out.println(answer1);
        String answer2 = separateChatAssistant.chat(1,"我是谁");
        System.out.println(answer2);
        String answer3 = separateChatAssistant.chat(2,"我是谁");
        System.out.println(answer3);
    }
}
